var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var Base64util = require("../../utils/base64.js");
var WxParse = require("../../lib/wxParse/wxParse.js")
var app = getApp();

Page({
  data: {
    hotelId: 0,
    hotel: "",
    rooms: [],
    images: [],
    coverImage: "",
    imageNumber: 1,
    notes: [],
    features: [],
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0,
    startDate: "",
    endDate: "",
    dates: 1
  },
  getData: function () {
    let that = this;

    var startDate = wx.getStorageSync("startDate");
    var endDate = wx.getStorageSync("endDate");
    var aDate, oDate1, oDate2, iDays
    aDate = endDate.split("-")
    console.log("aDate>>>:length-" + aDate.length + ",0-" + aDate[0] + ",1-" + aDate[1] + ",2-" + aDate[2])
    oDate1 = new Date();
    oDate1.setFullYear(aDate[0]);
    oDate1.setMonth(aDate[1] - 1);
    oDate1.setDate(aDate[2]);

    aDate = startDate.split("-")
    oDate2 = new Date();
    oDate2.setFullYear(aDate[0]);
    oDate2.setMonth(aDate[1] - 1);
    oDate2.setDate(aDate[2]);
    console.log("oDate2》》》", oDate2)
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  

    that.setData({
      startDate: startDate,
      endDate: endDate,
      dates: iDays
    })
    util.request(api.HotelDetail, { id: that.data.hotelId, startDate: startDate, endDate: endDate }).then(function (res) {
      if (res.code === '200') {
        that.setData({
          rooms: res.datas.hotelRooms,
          images: res.datas.images,
          hotel: res.datas.hotel,
          notes: res.datas.features,
          coverImage: res.datas.hotel.path,
          imageNumber: res.datas.images.length
        });

        var service = "", obj = "", features = res.datas.features;
        if (features != null && features.length > 0) {
          for (var i = 0; i < features.length; i++) {
            if (features[i].featuresName == '酒店设施') {
              obj = features[i].featuresValue.split(",")
            }
            else if (features[i].featuresName == '设施服务') {
              service = features[i].featuresValue;
            }
          }
        }

        that.setData({
          rooms: res.datas.hotelRooms,
          images: res.datas.images,
          hotel: res.datas.hotel,
          coverImage: res.datas.hotel.path,
          imageNumber: res.datas.images.length,
          features: res.datas.features,
          service: service,
          obj: obj
        });
        WxParse.wxParse('content', 'html', Base64util.Base64.decode(res.datas.hotel.info, "utf-8"), that, 5);


        wx.setNavigationBarTitle({
          //页面标题为路由参数
          title: res.datas.hotel.hotelName
        })
      }
    });
  },

  onLoad: function (options) {
    var that = this;
    console.log(options)
    that.setData({
      hotelId: options.id//options为页面路由过程中传递的参数
    })
    // 页面初始化 options为页面跳转所带来的参数
    app.init(options, that);
    this.getData();
    /** 
      * 获取系统信息 
      */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
    // var type=wx.getStorageSync("type");
    // this.setData({
    //   type: type
    // })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    app.getShareInfo(this);
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  previewImage: function () {
    var that = this;
    if (that.data.images.length > 1) {

    wx.previewImage({
      current: 1, // 当前显示图片的http链接  
      urls: that.data.images// 需要预览的图片http链接列表  
    })}
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  makePhoneCall: function (e) {
    var phone = this.data.hotel.telephone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  previewImage: function () {
    var that = this;
    wx.previewImage({
      current: 1, // 当前显示图片的http链接  
      urls: that.data.images// 需要预览的图片http链接列表  
    })
  },
  showDetail: function (e) {
    var id = e.currentTarget.id;
    var name = e.currentTarget.dataset.hotelname;
    wx.navigateTo({
      url: '/pages/hotelInfo/index?id=' + id + "&name=" + name
    })
  },
  fnOrder: function (e) {
    var room = e.currentTarget.dataset.item;
    var type = "hotel";
    var id = room.id;
    var startDate = "";
    var endDate = "";
    var url = "/pages/preOrder/index?type=" + type +
      "&id=" + id +
      "&startDate=" + startDate +
      "&endDate=" + endDate;
    wx.navigateTo({
      url: url,
    });

  },
  fnShare: function (e) {
    var path = "/pages/hotelDetail/hoteldetail"
    var id = this.data.hotelId;
    var imgUrl = this.data.hotel.path;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    app.fnShareGoods(path, id, imgUrl, this.data.hotel.hotelName, "hotel", this.data.hotel.address, price, marketPrice);
  },
  openMap: function (e) {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = that.data.hotel.latitude
        var longitude = that.data.hotel.longitude
        console.log("longitude", longitude);
        console.log("latitude", latitude);
        wx.openLocation({
          latitude: parseFloat(latitude),
          longitude: parseFloat(longitude),
          scale: 28
        })
      }
    })
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index-new',
    })
  }, fnCall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  fnDetail: function (e) {
    var content = e.currentTarget.dataset.content;
    
    wx.navigateTo({
      url: '/pages/shopdetail/index?content=' +content,
    })
  }
})